import { getCartList } from 'api/cart'
export default {
  namespaced: true,
  state: {
    // 真实的购物车列表嵌套过深,需要用getters简化
    cartInfoList: []
  },
  mutations: {
    setCartList (state, payload) {
      state.cartInfoList = payload
    }
  },
  getters: {
    // 购物车列表
    cartList (state) {
      const tempCartList = []
      state.cartInfoList.forEach((val) => {
        tempCartList.push(val.cartInfoList[0])
      })
      return tempCartList
    }
  },
  actions: {
    // 获取购物车列表
    async onGetCartList ({ commit }) {
      const { data: res } = await getCartList()
      commit('setCartList', res.data)
    }
  }
}
